package com.antjupt.servlet;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author Wizarder
 * @create 2021-05-25 11:12
 */
public class HelloServlet implements Servlet {

    public HelloServlet(){
        System.out.println("构造器hello,servlet");
    }
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("init方法里的hello,servlet");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    /**
     * 处理请求和响应
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("service里的hello,servlet");
        // 类型转换（因为它有getMethod()方法）
        HttpServletRequest  method = (HttpServletRequest) servletRequest;
        // 获取请求的方式
        String method1 = method.getMethod();//得到的字符串是大写的
//        System.out.println(method1);
        if("POST".equals(method1)){
            doPost();
        }else if("GET".equals(method1)){
            doGet();
        }
    }
    public void doGet(){
        System.out.println("做get请求方式的方法。");
        System.out.println("做get请求方式的方法。");
        System.out.println("做get请求方式的方法。");
    }
    public void doPost(){
        System.out.println("做post请求方式的方法");
        System.out.println("做post请求方式的方法");
        System.out.println("做post请求方式的方法");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
